/**
 *    Copyright (C) 2008, 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_TOOLS_TYPES_DISPATCHER_HPP
#define	_CUBEFS_CORE_TOOLS_TYPES_DISPATCHER_HPP

#include <cubefs/core/tools/types/list/typelist.hpp>
#include <cubefs/api/macro.hpp>
#include <cubefs/api/exceptions/dispatch.hpp>
#include <cubefs/api/exceptions/npe.hpp>

#include <iostream>

#define CUBEFS_SOURCE "cubefs/core/tools/types/dispatcher.hpp:" CUBEFS_AT

namespace cubefs {namespace tools {

using cubefs::api::DispatchException;
using cubefs::api::NullPointerException;

template <typename Obj> BigInt Ptr2Handle(const Obj *ptr) {
    return reinterpret_cast<BigInt>(ptr);
}

template <typename Obj> Obj* Handle2Ptr(BigInt handle, bool throwNPE = true) {
    if (throwNPE && handle == 0) {
        throw NullPointerException(CUBEFS_SOURCE, "Can't convert zero handle to pointer for object of type " + cubefs::api::TypeNameFactory<Obj>::name());
    }
    return reinterpret_cast<Obj*>(handle);
}

template <typename List> class TypeDispatcher;

template <typename Head, typename Tail>
class TypeDispatcher<TypeList<Head, Tail> > {
    static const Int Code = Head::First::Value;
    typedef typename Head::Second Type;

public:

    template <typename Functor>
    static void Dispatch(Int typeCode, void *ptr, Functor &functor) {
        //std::cout<<"Dipatch: "<<typeCode<<" "<<Code<<std::endl;
        if (typeCode == Code) {
            Type* obj = (Type*)ptr;
            functor(obj);
        }
        else {
            TypeDispatcher<Tail>::Dispatch(typeCode, ptr, functor);
        }
    }

    template <typename Functor>
    static void Dispatch(Int typeCode, BigInt handle, Functor &functor) {
        if (typeCode == Code) {
            Type *obj = Handle2Ptr<Type>(handle);
            functor(obj);
        }
        else {
            TypeDispatcher<Tail>::Dispatch(typeCode, handle, functor);
        }
    }
};

template <>
class TypeDispatcher<NullType> {
public:
    template <typename Functor>
    static void Dispatch(Int typeCode, void *ptr, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, typeCode);
    }

    template <typename Functor>
    static void Dispatch(Int typeCode, BigInt handle, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, typeCode);
    }
};


}}

#undef CUBEFS_SOURCE

#endif	//_CUBEFS_CORE_TOOLS_TYPES_DISPATCHER_HPP
